package com.wan37.client.message.received;

import com.google.protobuf.Message;
import com.googlecode.protobuf.format.JsonFormat;
import com.wan37.client.handler.ClientHandler;
import com.wan37.client.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.auth.Auth;
import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author : luoyong
 * @date : 2020-07-01 12:54
 **/
@Component(PtoNum.SRESPONSE + "")
public class AuthSResponse implements ReceiveMsg {
    private final static Logger logger = LoggerFactory.getLogger(AuthSResponse.class);
    private ClientHandler clientHandler = ClientHandler.getInstance();
    @Autowired
    private UserServiceCache userServiceCache;

    @Override
    public void execute(Message msg) {
        /**
         * 注册或者登录的返回信息都会以SResponse消息体返回.
         */
        Auth.SResponse sresp = (Auth.SResponse) msg;
        int code = sresp.getCode();
        String content = sresp.getContent();
        //对消息码code进行判断
        switch (code) {

            //账号未注册
            case Common.ACCOUNT_INEXIST:
                logger.info("账号未注册, content : {}", content);
                break;

            //登陆成功
            case Common.LOGIN_SUCCESS:
                logger.info("登陆成功, content : {}", content);
                clientHandler.current_scene = "启始之地";
                clientHandler.login_success = true;
                GameEntity.User user = sresp.getUser();
                clientHandler.selfId = user.getUserId();
                UserServiceCache.UserIn userIn = userServiceCache.new UserIn();
                userIn.setUser(user);
                logger.info("用户当前情况：{}", JsonFormat.printToString(user));
                userServiceCache.getUserInMap().put(user.getUserId(), userIn);
                //发送初始化消息
                clientHandler.sendUserLoginInitMsg(user.getUserId());
                break;

            //账号或者密码错误
            case Common.LOGIN_ERROR:
                logger.info("登录失败, content : {}", content);
                break;

            //账号已被注册
            case Common.ACCOUNT_ALREADY_EXIST:
                logger.info("账号已被注册, content : {}", content);
                break;

            //账号注册成功
            case Common.REGISTER_OK:
                logger.info("账号注册成功, content : {}", content);
                break;

            //重复登录
            case Common.REPEATED_LOGIN:
                logger.info("账号已经登录, 不允许重复登录, content : {}", content);
                break;

            //发送者未登录
            case Common.GAME_SENDER_OFFLINE:
                logger.info("账号未登陆, 请登陆后再发送消息. content : {}", content);
                break;

            //服务器接收消息成功
            case Common.GAME_MESSAGE_RECEIVE_SUCCESS:
                logger.info("服务器成功接收到消息! content : {}", content);
                break;

            default:
                logger.info("错误的 Code, code : {}", code);
                break;
        }
    }
}
